package sortpom.wrapper;

import org.jdom.Element;

import java.util.HashMap;
import java.util.Map;

import static sortpom.wrapper.ElementUtil.getDeepName;

/**
 * All elements from the chosen sort order (from predefined sort order or custom sort order) are placed in this map
 * along with an index that describes in which order the elements should be sorted.
 */
class ElementSortOrderMap {
    /** Contains sort order element names and their index. */
    private final Map<String, Integer> elementNameSortOrderMap = new HashMap<String, Integer>();

    /**
     * Add an Xml element to the map
     *
     * @param element   Xml element
     * @param sortOrder an index describing the sort order (lower number == element towards the start of the file)
     */
    public void addElement(Element element, int sortOrder) {
        final String deepName = getDeepName(element);
        elementNameSortOrderMap.put(deepName, sortOrder);

    }

    /** Returns true if element is in the map */
    public boolean containsElement(Element element) {
        String deepName = getDeepName(element);
        return elementNameSortOrderMap.containsKey(deepName);
    }

    /** Gets the index describing the sort order (lower number == element towards the start of the file) */
    public int getSortOrder(Element element) {
        String deepName = getDeepName(element);
        return elementNameSortOrderMap.get(deepName);
    }

}
